/*
** Copyright (c) 2002  Hughes Technologies Pty Ltd.  All rights
** reserved.
**
** Terms under which this software may be used or copied are
** provided in the  specific license associated with this product.
**
** Hughes Technologies disclaims all warranties with regard to this
** software, including all implied warranties of merchantability and
** fitness, in no event shall Hughes Technologies be liable for any
** special, indirect or consequential damages or any damages whatsoever
** resulting from loss of use, data or profits, whether in an action of
** contract, negligence or other tortious action, arising out of or in
** connection with the use or performance of this software.
**
**
** $Id: test_httpd.c,v 1.13 2004/05/09 18:06:24 bambi Exp $
**
*/

#include "config.h"
#include "httpd.h"
#include "common.h"

/*
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#ifdef _WIN32
#  include <getopt.h>
#else
#  include <sys/time.h>
#endif
*/


/*
** This is a static page of HTML.  It is loaded into the content
** tree using httpdAddStaticContent( ).
*/
#define test1_html "<h1>This is just a test</h1><br><form action=test2.html method=post><input type=text name=name size=10><input type=submit></form><br><a href=test2.html>test2</a>"

#define ember_code1 "printf(\"This is from ember in process %d\n\",getpid());"
#define ember_code "load \"/usr/local/nk/www/content.lib\"; dispatchWebPage(\"/index.html\");"

/*
** Below are 2 dynamic pages, each generated by a C function.  The first
** is a simple page that offers a little dynamic info (the process ID)
** and the setups up a test link and a simple form.
** 
** The second page processes the form.  As you can see, you can access
** the form data from within your C code by accessing the symbol table
** using httpdGetVariableByName() (and other similar functions).  You
** can also include variables in the string passed to httpdOutput( ) and
** they will be expanded automatically.
*/
void index_html(server)
	httpd	*server;
{
	/*
	httpdPrintf(server,
	    "Welcome to the httpd server running in process number %d<P>\n",
	    getpid());
	    */
	debug_libhttpd("index_html enter");
	httpdPrintf(server,
	    "Click <A HREF=/test1.html>here</A> to view a test page<P><br>");
	httpdPrintf(server,
	    "Click <A HREF=/login.html>here</A> to authenticate<P><br>");
	httpdPrintf(server,
	    "Or <A HREF=/wildcard/foo>here</A> for a test wildcard page<P><br>");
	httpdPrintf(server, "<P><FORM ACTION=test2.html METHOD=POST><br>");
	httpdPrintf(server, "Enter your name <INPUT NAME=name SIZE=10><br>");
	httpdPrintf(server, "<INPUT TYPE=SUBMIT VALUE=Click!><P></FORM><br>");
	debug_libhttpd("index_html leave");
	return;
}

void test2_html(server)
	httpd	*server;
{
	httpVar	*variable;

	/*
	** Grab the symbol table entry to see if the variable exists
	*/
	variable = httpdGetVariableByName(server, "name");
	if (variable == NULL)
	{
		httpdPrintf(server,"Missing form data!");
		return;
	}

	/*
	** Use httpdOutput() rather than httpdPrintf() so that the variable
	** embedded in the text is expanded automatically
	*/
	httpdOutput(server,"Hello $name");
}

void test3_html(server)
	httpd	*server;
{
	char	*path;

	path = httpdRequestPath(server);
	//httpdOutput(server,"Wilcard dynamic request received<P>");
	httpdPrintf(server,"<h3>Wilcard dynamic request received<h3>");
	httpdPrintf(server,"The requested path was %s<P>", path);
}


void login_html(server)
	httpd	*server;
{
	if (httpdAuthenticate(server, "LibHTTPD Test") == 0)
		return;
	httpdPrintf(server, "Your username is '%s'<P>\n",
		server->request.authUser);
	httpdPrintf(server, "Your password is '%s'<P>\n",
 		server->request.authPassword);
	httpdOutput(server, 
		"Click <A HREF=login2.html>here</A> to force reauthentication");
	httpdOutput(server, ".  Use a username = test password = 123");
}



void login2_html(server)
	httpd	*server;
{
	if (httpdAuthenticate(server, "LibHTTPD Test") == 0)
	{
		httpdOutput(server, "Authentication failure(1).");
		return;
	}
	if (strcmp(server->request.authUser, "test") != 0 ||
	    strcmp(server->request.authPassword, "123") != 0)
	{
		httpdForceAuthenticate(server, "LibHTTPD Test");
		httpdOutput(server, "Authentication failure (2).");
		return;
	}
	httpdOutput(server, "Your login was accepted.");
}


void myError404Handler(server, error)
	httpd	*server;
	int	error;
{
	httpdOutput(server,"Whoa there.  You hit a page that doesn't exist! <P><BR><BR>\n");
	httpdOutput(server,"Or in other words : <B>Error 404</B>\n\n");
}


int main_httpd(char* host, int port)
{
	httpd	*server;
	int result;
	int c;


	/*
	void *p;
  	skyeye_printf("%d\n",sizeof(httpd));
	p = malloc(2700);
	if(p == NULL)
		skyeye_printf("p failed\n");
	free(p);
	*/

	char str_tmp[100];
	snprintf(str_tmp,100,"%d%s",123,"hello");
	print("str_tmp = %s\n",str_tmp);
	
	server = httpdCreate(host,port);
	if (server == NULL)
	{
		skyeye_printf("Can't create server\n");
		return 1;
	}
	else
		skyeye_printf("httpCreate ok\n");
	//httpdSetAccessLog(server, stdout);
	//httpdSetErrorLog(server, stdout);

	debug_libhttpd("after httpdCreate");
	/*
	** We are fussy and don't want the default Error 404 page
	*/
	httpdSetErrorFunction(server,404, myError404Handler);


	/*
	** Setup some content for the server
	*/
#if 1
	httpdAddCContent(server,"/", "index.html", HTTP_TRUE, 
		NULL, index_html);
	httpdAddCContent(server,"/", "test2.html", HTTP_FALSE, 
		NULL, test2_html);
	httpdAddCContent(server,"/", "login.html", HTTP_FALSE,
		NULL, login_html);
	httpdAddCContent(server,"/", "login2.html", HTTP_FALSE,
		NULL, login2_html);
	httpdAddCWildcardContent(server,"/wildcard", NULL, test3_html);
	httpdAddStaticContent(server, "/", "test1.html", HTTP_FALSE,
		NULL, test1_html);
	httpdAddEmberContect(server,"/","ember.html", HTTP_FALSE, 
		NULL, ember_code);
#else
	httpdAddCContent(server,"/", "test2.html", HTTP_FALSE, 
		NULL, test2_html);
	httpdAddStaticContent(server, "/", "index.html", HTTP_FALSE,
		NULL, test1_html);
#endif

	/*
	** Go into our service loop
	*/

	skyeye_printf("in main_httpd\n");

	while(1 == 1)
	{
		skyeye_printf("while 1\n");
		
		result = httpdGetConnection(server);
		if (result == 0)
		{
			skyeye_printf("Timeout ... \n");
			continue;
		}
		else if (result < 0)
		{
			skyeye_printf("Error ... \n");
			continue;
		}
		else
			skyeye_printf("Got connection\n");

		if(httpdReadRequest(server) < 0)
		{
			debug_libhttpd("httpdReadRequest < 0");
			httpdEndRequest(server);
			continue;
		}
		httpdProcessRequest(server);
		httpdEndRequest(server);
	}
}
